package com.ims.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ims.dao.DeviceMapper;
import com.ims.pojo.*;
import com.ims.service.DeviceService;
import com.ims.util.DateUtil;
import com.ims.util.PageResult;
import com.ims.util.QueryPageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @author: zk
 * @date: 2022/9/4 15:42
 */
@Service
public class DeviceServiceImpl extends ServiceImpl<DeviceMapper, Device> implements DeviceService {

    @Autowired
    private DeviceMapper deviceMapper;

    @Override
    public boolean add(Device device) {
        if (device == null || !StringUtils.hasText(device.getDeviceId()) || !StringUtils.hasText(device.getDeviceInfo())) {
            return false;
        }
        QueryWrapper<Device> wrapper = new QueryWrapper<>();
        wrapper.eq("device_id", device.getDeviceId());
        Device one = deviceMapper.selectOne(wrapper);
        // 该设备已存在，更新设备信息
        if (one != null) {
            deviceMapper.update(device, wrapper);
        } else {
            deviceMapper.insert(device);
        }
        return true;
    }

    /**
     * 分页查询
     * @param queryPageBean
     * @return
     */
    @Override
    public PageResult<JSONObject> findPage(QueryPageBean queryPageBean) {
        QueryWrapper<Device> wrapper = new QueryWrapper<>();
        if (StringUtils.hasText(queryPageBean.getQueryString())) {
            wrapper.like("device_name", queryPageBean.getQueryString());
        }
        Page<HashMap> page = new Page<>(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        List<HashMap> deviceList = deviceMapper.getDeviceInfoAndStatus(page);

        // 字符串转json
        ArrayList<JSONObject> result = new ArrayList<>();
        for (HashMap device : deviceList){
            JSONObject item = new JSONObject();
            item.put("device_id", device.get("device_id"));
            item.put("device_info", JSONObject.parse(device.get("device_info").toString()));
            item.put("status_info", JSONObject.parse(device.get("status_info").toString()));
            item.put("cpu_usage", device.get("cpu_usage"));
            item.put("ram_usage", device.get("ram_usage"));
            item.put("network_send_usage", device.get("network_send_usage"));
            item.put("network_receive_usage", device.get("network_receive_usage"));
            item.put("cpu_status", device.get("cpu_status"));
            item.put("ram_status", device.get("ram_status"));
            item.put("send_usage_status", device.get("send_usage_status"));
            item.put("receive_usage_status", device.get("receive_usage_status"));
            item.put("event_time", DateUtil.parse(device.get("event_time")));
            result.add(item);
        }

        return new PageResult<>(page.getTotal(), result);
    }
}
